<?php
require_once 'Util.php';
require_once 'Rubric.php';
/**
 * RubricsManager
 * Created on 27.09.2009 18:15:16
 * @author nikolai
 */
class RubricsManager {
    var $root;
    var $logger;

    var $rubricsPath;
    var $rubricIndicator;
    var $rubrics;

    function RubricsManager($root) {
        $this->root = $root;
        $this->logger = new Logger($root, "RubricsManager");
        $this->rubricsPath = $root."conf/rubrics.txt";
        $this->rubricIndicator = "!";
    }

    function readRubrics() {
        $lines = file($this->rubricsPath);
        $this->rubrics = array();

        $rubricsCount = 0;
        $currentRubric;

        foreach ($lines as $line) {
            $line = cutNewLine($line);
            $isRub = $this->isRubric($line);

            $this->logger->applogDebug("reading line '".$line."'");

            if ($this->isRubric($line)) {
                // add new rubric
                $currentRubric = new Rubric($this->root, $this->cutRubricsPrefix($line));
                $this->rubrics[$rubricsCount++] = $currentRubric;
            } else {
                // add subrubric to cur rubric
                $currentRubric->addSubrubric($line);
            }
        }

        $this->logger->applogDebug("finished reading ".count($this->rubrics)." rubrics");
    }

    function getRubrics() {
        return $this->rubrics;
    }

    function getSubRubrics($rubric) {
        return $rubric->getSubrubrics();
    }
    
    private function isRubric($line) {
        if (strcmp(
                $this->rubricIndicator,
                substr($line, 0, strlen($this->rubricIndicator))) == 0) {
            return true;
        }
        return false;
    }

    private function cutRubricsPrefix($str) {
        $str = trim($str);

        // if first char is $this->rubricIndicator, cut it
        if ($this->isRubric($str)) {
            return substr($str, 1);
        }

        return $str;
    }
}
?>
